package com.mcjtf.prefix;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mcjtf.prefix.bridge.NameTagHook;
import com.mcjtf.prefix.bridge.PlaceholderHook;
import com.mcjtf.prefix.bridge.PrefixPlaceholders;
import com.mcjtf.prefix.bridge.VaultHook;
import com.mcjtf.prefix.commands.AdminCommand;
import com.mcjtf.prefix.commands.MainCommand;
import com.mcjtf.prefix.commands.PlayerCommand;
import com.mcjtf.prefix.listener.PlayerListener;
import com.mcjtf.prefix.listener.PrefixListener;
import com.mcjtf.prefix.service.ConfigService;
import com.mcjtf.prefix.service.PrefixService;
import com.mcjtf.prefix.update.UpdateChecker;
import lombok.Getter;
import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.List;

public class PrefixManager extends JavaPlugin {
    @Getter
    private static PrefixManager instance;
    public final List<String> temp = Lists.newArrayList();
    public final HashMap<String, String> format = Maps.newHashMap();
    public final HashMap<String, List<String>> groups = Maps.newHashMap();
    @Getter
    private ConfigService configService;
    @Getter
    private PrefixService prefixService;
    @Getter
    private VaultHook vaultHook;
    @Getter
    private PlaceholderHook placeholderHook;
    @Getter
    private NameTagHook nameTagHook;

    public void onEnable() {
        instance = this;

        configService = new ConfigService();
        prefixService = new PrefixService();

        getServer().getPluginManager().registerEvents(new PrefixListener(), this);
        getServer().getPluginManager().registerEvents(new PlayerListener(), this);

        new MainCommand("prm", this).register(new AdminCommand()).register(new PlayerCommand());

        loadHookPlugin();
        startSaveTask();

        new Metrics(this, 2764);
        if (ConfigService.checkUpdate) {
            new UpdateChecker(this).fetch();
        }

        getLogger().info("插件已加载.");
    }

    public void onDisable() {
        configService.saveAll();
        prefixService.getUserMap().clear();
        temp.clear();
        getLogger().info("玩家数据已保存");
    }

    public String msg(String msg) {
        return msg == null ? "" : msg.replace('&', '§');
    }

    public boolean isInt(String msg) {
        return msg != null && msg.matches("^[0-9]*$");
    }

    private void loadHookPlugin() {
        vaultHook = new VaultHook();
        if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
            placeholderHook = new PlaceholderHook();
            new PrefixPlaceholders().register();
            getLogger().info("已检测到PlaceholderAPI插件");
        }
        if (getServer().getPluginManager().isPluginEnabled("ProtocolLib") && ConfigService.USE_NAMETAG) {
            nameTagHook = new NameTagHook();
        }
    }

    private void startSaveTask() {
        getServer().getScheduler().runTaskTimer(this, () -> {
            getLogger().info("正在保存玩家数据...");
            configService.saveAll();
            for (String name : temp) {
                prefixService.getUserMap().remove(name);
            }
            temp.clear();
            getLogger().info("玩家数据已保存.");
        }, ConfigService.SAVE_PERIOD * 20, ConfigService.SAVE_PERIOD * 20);
    }
}